package com.py.credit.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.dao.DownloadDao;
import com.py.credit.service.DownloadService;
import com.py.framework.core.common.Page;

/**
 * @Company: WWW.xypy.com.cn
 * @ClassName DownloadServiceImpl
 * @Description: 下载文件业务实现类
 * @Author: YaoS
 * @Create: 2018-09-26 17:00
 **/
@Service
public class DownloadServiceImpl implements DownloadService {

	public static String F_PATH = "";
	public static String F_NAME = "";
	public static String F_TYPE = "";
	public static int ONE = 0;
	public static int TWO = 0;
	@Autowired
	DownloadDao downloadDao;

	@Override
	public List<Map<String, Object>> selectDownForPage(String type, Page page) {
		return downloadDao.selectDownForPage(type, page);
	}

	@Override
	public boolean updateFTP(ArrayList<String> arrayList) {
		// arrayList是服务器中的文件
		for (String s : arrayList) {
			ONE = s.lastIndexOf("/");
			if (ONE > 0) {
				F_PATH = s.substring(0, ONE);
				F_NAME = s.substring((ONE + 1), s.length());
				TWO = F_PATH.lastIndexOf("/");
				F_TYPE = F_PATH.substring(TWO + 1, F_PATH.length());
				// 去数据库中查询对应文件
				List<Map<String, Object>> list = downloadDao.selectDocument(F_NAME, F_TYPE);
				// 如果数据库中不存在，就插入
				if (list.size() <= 0) {
					// 对路径进行包装
					StringBuffer s2 = new StringBuffer();

					try {
						String[] strings = s.split("/");

						for (String s1 : strings) {
							if (s1.length() > 0) {
								s1 = URLEncoder.encode(s1, "GBK");
								s2 = s2.append("/").append(s1);
							}
						}
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					downloadDao.insertDocument(F_NAME, s2.toString(), F_TYPE);
				}
			}
		}
		return false;
	}

	@Override
	public void updateSUM(String name, String type) {
		downloadDao.updateSUM(name, type);
	}

}
